package com.cloud.provider.controller;

import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.provider.entity.Person;

@RestController
public class FirstController {

	@RequestMapping(value = "/findPerson", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public Person findPerson(@RequestBody Person person, HttpServletRequest req) {
		person.setUrl(req.getRequestURL().toString());
		Random rand = new Random();
		int randNum = rand.nextInt(100);
		if (randNum % 3 == 0) {
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return person;
	}

	@GetMapping("/get/{personId}")
	public Person sayHello(@PathVariable("personId") Integer personId, HttpServletRequest req) {
		Person person = new Person();
		person.setPersonId(personId);
		person.setName("安徽合肥");
		person.setMail("baidu@qq.com");
		person.setAddress("");
		person.setUrl(req.getRequestURL().toString());
		Random rand = new Random();
		int randNum = rand.nextInt(100);
		if (randNum % 3 == 0) {
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return person;
	}

}
